package com.novasoftware.novagooglemap.activity;

import java.util.List;

import org.json.JSONException;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.Toast;

import com.novasoftware.novagooglemap.R;
import com.novasoftware.novagooglemap.adapter.LoginAdpter;
import com.novasoftware.novagooglemap.db.builder.AgentBuilder;
import com.novasoftware.novagooglemap.db.entity.Agent;
import com.novasoftware.novagooglemap.framework.activity.BaseActivity;
import com.novasoftware.novagooglemap.infrastructure.util.Caller;
import com.novasoftware.novagooglemap.infrastructure.util.RequestCache;
import com.novasoftware.novagooglemap.infrastructure.util.WSError;
import com.novasoftware.novagooglemap.service.JSONServiceBase;

public class LoginActivity extends BaseActivity {

	private GridView _gvList;

	private ImageButton _btnAdministrator;

	private List<Agent> _dsAgents;

	private LoginAdpter _adapter;
	
	private JSONServiceBase<Agent> _service;
	
	private AgentBuilder _builder;

	@Override
	protected void InitializeService() {
		RequestCache mRequestCache = new RequestCache();

		Caller.setRequestCache(mRequestCache);
		_builder = new AgentBuilder();
		_service = new JSONServiceBase<Agent>(LoginActivity.this, _builder);
	}

	@Override
	protected boolean IsHiderUI() {
		return false;
	}

	@Override
	protected int getContent() {
		return R.layout.login;
	}

	@Override
	protected void LocalizeControls() {
		_gvList = (GridView) findViewById(R.id.gvList);
		_btnAdministrator = (ImageButton) findViewById(R.id.btnAdministrator);
	}

	@Override
	protected void PopulateData() {
	  try {
		_dsAgents = _service.getJSonList("id+name+url+image+rating+artist_name/album/json/?n=20&order=ratingweek_desc");
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (WSError e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

	@Override
	protected void RegisterAdapter() {
		_adapter = new LoginAdpter(LoginActivity.this, R.layout.login_item, _dsAgents);
	}

	@Override
	protected void InitializeControls() {
		_gvList.setAdapter(_adapter);
	}

	@Override
	protected void RegisterEvents() {
		_btnAdministrator.setOnClickListener(new AdministratorClickListener());
		_gvList.setOnItemClickListener(new gvListItemClickListener());
	}

	class gvListItemClickListener implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
		   
		}
		
	}
	
	class AdministratorClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Toast.makeText(LoginActivity.this, "Administrator Click",
					TRIM_MEMORY_BACKGROUND).show();
		}
	}
}
